package cn.lili.util;

import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * html模板消息
 */
@Slf4j
public class TemplateHtml {


    public static String  balanceAddHtml = "<div style='padding:0px 5px 0 5px'>\n" +
            "  <div style='border-bottom:1px solid #F7F7F7;padding-bottom:10px;font-size:18px;font-weight:bold;color:#3D3D3D'>${title}</div>\n" +
            "  <div style='text-align:center;border-bottom:1px solid #F7F7F7;padding:20px 0px;'>\n" +
            "   \n" +
            "    <div style='${moneyColor};font-size:28px;font-weight:bold'>${money}</div>\n" +
            "  </div>\n" +
            "  <div style='padding:10px 0px;display:flex;flex-direction:row;justify-content:space-between;align-items:center'>\n" +
            "  <div style='color:#8D8D8D;'>到账时间</div>\n" +
            "  <div style='color:#3E3E3E;'>${payTime}</div>\n" +
            "  </div>\n" +
            "  <div style='padding:10px 0px;display:flex;flex-direction:row;justify-content:space-between;align-items:center'>\n" +
            "  <div style='color:#8D8D8D;'>订单号</div>\n" +
            "  <div style='color:#3E3E3E;max-width:180px;text-align:right'>${orderNo}</div>\n" +
            "  </div>\n" +
            "  <div style='padding:10px 0px;display:flex;flex-direction:row;justify-content:space-between;align-items:center'>\n" +
            "  <div style='color:#8D8D8D;'>订单类型</div>\n" +
            "  <div style='color:#3E3E3E;'>${orderType}</div>\n" +
            "  </div>\n" +
            "  <div style='padding:10px 0px;display:flex;flex-direction:row;justify-content:space-between;align-items:center'>\n" +
            "  <div style='color:#8D8D8D;'>支付方式</div>\n" +
            "  <div style='color:#3E3E3E;'>${payType}</div>\n" +
            "  </div>\n" +
            "  <div style='border-top:1px solid #F7F7F7;margin-top:10px;padding-top:10px;display:flex;flex-direction:row;justify-content:flex-end;align-items:center'>\n" +
            "      <div><img src='https://feiyu-store.oss-cn-beijing.aliyuncs.com/discover/fygroup.jpg' width='108' height='21'></div>\n" +
            "  </div>\n" +
            "</div>";


    public static String  balancePayHtml = "<div style='padding:0px 5px 0 5px'>\n" +
            "  <div style='border-bottom:1px solid #F7F7F7;padding-bottom:10px;font-size:18px;font-weight:bold;color:#3D3D3D'>${title}</div>\n" +
            "  <div style='text-align:center;border-bottom:1px solid #F7F7F7;padding:20px 0px;'>\n" +
            "   \n" +
            "    <div style='${moneyColor};font-size:28px;font-weight:bold'>${money}</div>\n" +
            "  </div>\n" +
            "  <div style='padding:10px 0px;display:flex;flex-direction:row;justify-content:space-between;align-items:center'>\n" +
            "  <div style='color:#8D8D8D;'>支付时间</div>\n" +
            "  <div style='color:#3E3E3E;'>${payTime}</div>\n" +
            "  </div>\n" +
            "  <div style='padding:10px 0px;display:flex;flex-direction:row;justify-content:space-between;align-items:center'>\n" +
            "  <div style='color:#8D8D8D;'>订单号</div>\n" +
            "  <div style='color:#3E3E3E;max-width:180px;text-align:right'>${orderNo}</div>\n" +
            "  </div>\n" +
            "  <div style='padding:10px 0px;display:flex;flex-direction:row;justify-content:space-between;align-items:center'>\n" +
            "  <div style='color:#8D8D8D;'>订单类型</div>\n" +
            "  <div style='color:#3E3E3E;'>${orderType}</div>\n" +
            "  </div>\n" +
            "  <div style='padding:10px 0px;display:flex;flex-direction:row;justify-content:space-between;align-items:center'>\n" +
            "  <div style='color:#8D8D8D;'>支付方式</div>\n" +
            "  <div style='color:#3E3E3E;'>${payType}</div>\n" +
            "  </div>\n" +
            "  <div style='border-top:1px solid #F7F7F7;margin-top:10px;padding-top:10px;display:flex;flex-direction:row;justify-content:flex-end;align-items:center'>\n" +
            "      <div><img src='https://feiyu-store.oss-cn-beijing.aliyuncs.com/discover/fygroup.jpg' width='108' height='21'></div>\n" +
            "  </div>\n" +
            "</div>";

    public static String  couponReceiveHtml = "<div style='padding:0px 5px 0 5px'>\n" +
            "  <div style='border-bottom:1px solid #F7F7F7;padding-bottom:10px;font-size:18px;font-weight:bold;color:#3D3D3D'>${title}</div>\n" +
            "  <div style='padding:10px 0px;display:flex;flex-direction:row;justify-content:space-between;align-items:center'>\n" +
            "  <div style='color:#8D8D8D;'>到账时间</div>\n" +
            "  <div style='color:#3E3E3E;'>${payTime}</div>\n" +
            "  </div>\n" +
            "  <div style='padding:10px 0px;display:flex;flex-direction:row;justify-content:space-between;align-items:center'>\n" +
            "  <div style='color:#8D8D8D;'>订单号</div>\n" +
            "  <div style='color:#3E3E3E;max-width:180px;text-align:right'>${orderNo}</div>\n" +
            "  </div>\n" +
            "  <div style='padding:10px 0px;display:flex;flex-direction:row;justify-content:space-between;align-items:center'>\n" +
            "  <div style='color:#8D8D8D;'>获取途径</div>\n" +
            "  <div style='color:#3E3E3E;'>${source}</div>\n" +
            "  </div>\n" +
            "  <div style='border-top:1px solid #F7F7F7;margin-top:10px;padding-top:10px;display:flex;flex-direction:row;justify-content:flex-end;align-items:center'>\n" +
            "      <div><img src='https://feiyu-store.oss-cn-beijing.aliyuncs.com/discover/fygroup.jpg' width='108' height='21'></div>\n" +
            "  </div>\n" +
            "</div>";

    public static String getBalanceAddHtml(String title,String money,String payTime,String orderNo,
                                           String orderType, String payType,String moneyColor){
        if(StringUtil.isNullOrEmpty(moneyColor)){
            moneyColor = "color:#060606";
        }
        return balanceAddHtml.replace("${title}",title)
                .replace("${money}",money)
                .replace("${payTime}",payTime)
                .replace("${orderNo}",orderNo)
                .replace("${orderType}",orderType)
                .replace("${payType}",payType)
                .replace("${moneyColor}",moneyColor);
    }

    public static String getBalancePayHtml(String title,String money,String payTime,String orderNo,
                                           String orderType, String payType,String moneyColor){
        if(StringUtil.isNullOrEmpty(moneyColor)){
            moneyColor = "color:#060606";
        }
        return balancePayHtml.replace("${title}",title)
                .replace("${money}",money)
                .replace("${payTime}",payTime)
                .replace("${orderNo}",orderNo)
                .replace("${orderType}",orderType)
                .replace("${payType}",payType)
                .replace("${moneyColor}",moneyColor);
    }

    public static String getCouponReceiveHtml(String title,String payTime,String orderNo,
                                              String source){
        return couponReceiveHtml.replace("${title}",title)
                .replace("${payTime}",payTime)
                .replace("${orderNo}",orderNo)
                .replace("${source}",source);
    }

    /**
     * 发送余额消息
     * @param type  0 支付   1 接收
     */
    public static TemplateMessage sendMessageForHtml(String title,String money,String orderSn, String orderType,
                                               String payType, int type) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

            if(StringUtil.isNullOrEmpty(title)){
                title = "余额支付通知";
            }
            if ("ALIPAY".equals(payType) || "0".equals(payType)) {
                payType = "支付宝";
            }else if ("WECHAT".equals(payType)  || "1".equals(payType)) {
                payType = "微信";
            }
            String msg = "";
            if (type == 0) {
                msg = getBalancePayHtml(title,money,
                        sdf.format(new Date()),orderSn,orderType, payType,"color:#F7BE00");
            }else {
                msg = getBalanceAddHtml(title,money,
                        sdf.format(new Date()),orderSn,orderType, payType,"color:#F7BE00");
            }

            return new TemplateMessage(title, "",1,msg,new ArrayList<>());

        }catch(Exception e) {
            log.error("发送余额消息失败", e);
        }
        return null;
    }

    /**
     * 发送优惠券消息
     */
    public static TemplateMessage sendCouponMessageForHtml(String title, String orderSn, String source) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

            if(StringUtil.isNullOrEmpty(title)){
                title = "返券到账";
            }
            String msg = getCouponReceiveHtml(title,sdf.format(new Date()),orderSn, source);

            return new TemplateMessage(title, "",1,msg,new ArrayList<>());

        }catch(Exception e) {
            log.error("发送返券消息失败", e);
        }
        return null;
    }
}
